package com.bailiban.sms.consumer;

import com.aliyuncs.exceptions.ClientException;
import com.bailiban.sms.utils.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author zhulang
 * @Classname SmsConsumer
 * @Description SmsConsumer
 */
@Component
@RabbitListener(queues = "sms")
public class SmsConsumer {
    @Value("${aliyun.sms.template_code}")
    private String templateCode;
    @Value("${aliyun.sms.sign_name}")
    private String signName;
    @Autowired
    private SmsUtil smsUtil;

    @RabbitHandler
    public void getMsg(Map<String, String> map) {
        System.out.println("手机号：" + map.get("phone"));
        System.out.println("验证码：" + map.get("code"));
        try {
            smsUtil.sendSms(map.get("phone"), templateCode, signName, "{\"code\":\"" + map.get("code") + "\"}");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
